// pages/search/search.js
var api = require('../../utils/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      categoryList:[],
      albumList:[],
      keyword:'',
      audioList:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({keyword:options.keyword});
    this.getAlbumByKeywords();


  //  按照输入查询所有符合输出的album

  //  点击后将album信息和category信息缓存，并跳转至play并从第一首开始播放

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    loadAlbumData(){
        var promise = (fun)=>{
            return new Promise((resolve,reject)=>{
                fun(resolve,reject);
            })}
        var p1 = promise((resolve,reject)=>{
            api.get({
                url:'portal/categories',
                data:{},
                success:data=>{
                    console.log(data);
                    this.setData({categoryList:data.data.list});
                    resolve();
                },
                fail:data=>{
                    reject();
                }
            })
        });
        var p2 = promise((resolve,reject)=>{
            api.get({
                url: 'portal/album',
                data: {
                    // order:'-published_time'
                },
                success: data => {
                    console.log(data);
                    var portalAlbumList = data.data.portalAlbumList;
                    var userRbacList = wx.getStorageSync("user").rbac||''
                    var finalPortalAlbumList = [];
                    for(var album in portalAlbumList){
                        var _rbac = portalAlbumList[album].rbac.split(",");
                        for(var _r in _rbac){
                            //只要存在当前用户权限与专辑所要求权限交集
                            if(userRbacList.indexOf(_rbac[_r])!= -1){
                                finalPortalAlbumList.push(portalAlbumList[album]);
                                break;
                            }
                        }
                    }
                    this.setData({albumList:finalPortalAlbumList});
                    resolve();
                },
                fail:data=>{
                    reject();
                }
            });
        })
        Promise.all([p1,p2]).then(()=>{
            var list = this.data.categoryList;
            var zjlist = this.data.albumList;

            for(var i=0;i<list.length;i++){
                for(var n=0;n<zjlist.length;n++){
                    if(zjlist[n].cid == list[i].id){
                        console.log(list[i]);
                        if(list[i].albumList == null){
                            list[i].albumList = [];
                        }
                        list[i].albumList.push(zjlist[n]);
                    }
                }
            }



            this.setData({categoryList:list,audioList:files})
            console.log(this.data.categoryList);
        })
    },
    inputConfirm:function(e){
        if((e.detail.value||'').trim() != ''){
            this.setData({keyword:e.detail.value});
            this.getAlbumByKeywords();
        }

    },getAlbumByKeywords:function(e){
        api.get({
            url:'portal/album',
            data:{
                'keyword':this.data.keyword
            },
            success:(data)=>{
                this.setData({
                    keyword:this.data.keyword,
                    albumList:data.data.portalAlbumList
                })
                console.log(this.data.albumList);
            }
        })
    },
    play:function(e){
        var album_index = e.currentTarget.dataset.id;
        var albumList = this.data.albumList;
        var files = albumList[album_index].more?albumList[album_index].more.files:[];

        for(var i in files){
            if(files[i].name != ''&& files[i].name.indexOf(".mp3")>0){
                var _name = files[i].name;
                files[i].name = _name.split('.mp3')[0];
            }
        }
        wx.setStorageSync("album",{
            "albumInfo": this.data.albumList[album_index],
            "audiolist":files,
            "curAudioIndex":0
        });

        //    跳转tabbar页面
        wx.switchTab({
            url: '/pages/play/play',
            success: function(e){
                console.log("跳转完成");
            }
        });
    }
})